package com.yf.compareYFJX;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSONObject;
import com.yf.compareYFJX.ecpModel.queryChannels.ChannelsRequest;
import com.yf.compareYFJX.ecpModel.queryChannels.ChannelsResult;
import com.yf.compareYFJX.ecpModel.queryGoods._openx_head;
import com.yf.compareYFJX.excelModel.ChannelRows;
import java.util.ArrayList;
import java.util.List;

/**
 * 取渠道差集
 */
public class SaveChannels {

    /**
     * 查询ECP益丰精选下所有子渠道的接口
     */
    private final static String ECPCHANNELSURI = "https://ecp.yifengx.com/ecp-boss-new/openx/boss/channelService/listChildrenChannelByTopCode";


    public static void main(String[] args) throws Exception {

        _openx_head openx_head = CompareApplication.getOpenxHead();
        //1.查询ecp益丰精选下所有的渠道  生产渠道数：6009
        HttpUtils queryEcpChann = new HttpUtils(ECPCHANNELSURI, 5000, true);
        ChannelsRequest channelsRequest = new ChannelsRequest();
        channelsRequest.set_openx_head(openx_head);
        channelsRequest.setTopChannelCode("YFJX");
        String ECPchannels = queryEcpChann.post(JSONObject.toJSONString(channelsRequest));

        //创建写入excel流
        String fileName = "F:\\yf\\work\\比对YFJX\\YFJXCompare\\YFJXChannelRows.xlsx";
        // 这里 需要指定写用哪个class去写
        ExcelWriter excelWriter = EasyExcel.write(fileName).build();
        //设定好sheet名称
        WriteSheet writeSheet = EasyExcel.writerSheet(0, "益丰精选渠道集").head(ChannelRows.class).build();

        //拿到所有渠道集合
        List<ChannelsResult> channelsResults = JSONObject.parseArray(ECPchannels, ChannelsResult.class);
        List<ChannelRows> channelRows = new ArrayList<>();
        for (int i = 0; i < channelsResults.size(); i++) {
            ChannelsResult channel = channelsResults.get(i);
            String channelCode = channel.getChannelCode();
            ChannelRows channelRow = new ChannelRows();
            channelRow.setChannelCode(channelCode);
            channelRow.setIndex(i);
            channelRows.add(channelRow);
        }
        excelWriter.write(channelRows, writeSheet);
        excelWriter.finish();
    }

}
